package controllers;

import java.util.Date;
import java.util.List;

import org.codehaus.jackson.node.ObjectNode;

import models.Resource;
import models.ResourceType;
import models.Setting;
import play.data.Form;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import services.ConfigurationService;
import services.SettingService;
import services.SynchronizationService;
import views.html.configs.*;

public class Configurations extends Controller {
	
	private static final SettingService settingService = SettingService.getService();
	private static final SynchronizationService synchService = SynchronizationService.getService();
	private static final ConfigurationService configurationService = ConfigurationService.getService();
	
	public static Result listConfigs() {
		List<Resource> configs = configurationService.getAllConfigs();
		return ok(list.render(configs));
	}
	
	public static Result addConfig() {
		Form<Resource> configForm = new Form<Resource>(Resource.class);
		List<Resource> configs = configurationService.getAllConfigs();
		return ok(add.render(configForm, configs));
	}
	
	public static Result editConfig(long configId) {
		Resource config = configurationService.getConfig(configId);
		if (config != null) {
			Form<Resource> form = Form.form(Resource.class).fill(config);
			String stringConfig = configurationService.getConfigFileContent(config);
			form.data().put("config", stringConfig);
			List<Resource> configs = configurationService.getAllConfigs();
			return ok(edit.render(config, form, configs));
		} else {
			return TODO;
		}
	}
	
	public static Result createConfig() {
		Resource config = Form.form(Resource.class).bindFromRequest().get();
		String stringConfig = Form.form().bindFromRequest().get("config");
		config.setCreateDate(new Date());
		config.setType(ResourceType.TestConfiguration);
		config.setLastModified(config.getCreateDate());
		configurationService.saveConfig(config, stringConfig);
		return redirect(controllers.routes.Configurations.listConfigs());
	}
	
	public static Result updateConfig(long configId) {
		Resource config = configurationService.getConfig(configId);
		Resource newConfig = Form.form(Resource.class).bindFromRequest().get();
		String stringConfig = Form.form().bindFromRequest().get("config");
		config.setLastModified(new Date());
		config.setDescription(newConfig.getDescription());
		config.setName(newConfig.getName());
		configurationService.saveConfig(config, stringConfig);
		return redirect(controllers.routes.Configurations.editConfig(configId));
	}
	
	public static Result synchronizeConfigs() {
		synchService.synchronizeConfigurations(settingService.getSetting(Setting.REMOTE_CONFIG_HOME).getStringValue());
		return redirect(controllers.routes.Configurations.listConfigs());
	}
	
	public static Result getConfigContentRaw(long configId) {
		ObjectNode result = Json.newObject();
		if (configurationService.configExists(configId)) {
			Resource config = configurationService.getConfig(configId);
			result.put("content", configurationService.getConfigFileContent(config));
			result.put("remote", config.isRemote());
			return ok(result);
		} else {
			result.put("content", "");
			result.put("remote", false);
			return notFound(result);
		}
	}
	
}
